unit Thrad_LoadData;

interface

uses
  Classes,SysUtils,Class_KzThrad,AdvGrid,Uni,UniConnct,SyncObjs,Class_Data;

type
  TThradLoadData = class(TKzThrad)
  private
  public
    FRealGrid:TAdvStringGrid;  //&
    FCSInDATA:TCriticalSection;//&
    FDATATYPE:Integer;
  protected
    procedure Execute; override;
  end;

implementation
uses
  ActiveX,Class_KzUtils,Class_AppUtil;

procedure TThradLoadData.Execute;
var
  I:Integer;
  cSQL :string;
  cDATA:TDATA;
  cUniC:TUniConnection;
begin
  try
    CoInitialize(nil);
    if FCSInDATA<>nil then FCSInDATA.Enter;

    try
      cUniC:=UniConnctEx.GetConnection(CONST_MARK_DATA);
      try
        cUniC.StartTransaction;
        //-<
        cSQL := 'DELETE FROM TBL_DATA WHERE DATA_TYPE=%d';
        cSQL := Format(cSQL,[FDATATYPE]);
        TDATA.ExecuteSQL(cSQL,cUniC);
        
        cDATA:=TDATA.Create;

        with FRealGrid do
        begin
          GetMaxProgress(FRealGrid.RowCount-1);
          for I:=1 to FRealGrid.RowCount-1 do
          begin
            GetOneProgress(I+1);

            cDATA.DATACODE:=Trim(Cells[1,I]);
            cDATA.DATAVAL :=TKzUtils.TextToFloat(Trim(Cells[2,I]));
            cDATA.DATATYPE:=FDATATYPE;
            cDATA.InsertDB(cUniC);
          end;
        end;

        FreeAndNil(cDATA);        
        //->
        cUniC.Commit;
      except
        on E:Exception do
        begin
          cUniC.Rollback;
          raise Exception.Create(E.Message);
        end;
      end;
    finally
      FreeAndNil(cUniC);
    end;

    GetEndProgrees;
  finally
    if FCSInDATA<>nil then FCSInDATA.Leave;
    CoUninitialize;
  end;
end;

end.
